package com.huawei.su;

/**
 * https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=295&tqId=23257&ru=%2Fpractice%2Ff95dcdafbde44b22a6d741baf71653f6&qru=%2Fta%2Fformat-top101%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj
 */
public class BM10 {

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
        ListNode listNode6 = new ListNode(6);
        ListNode listNode7 = new ListNode(7);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode6;

        listNode4.next = listNode5;
        listNode5.next = listNode6;

        listNode6.next = listNode7;

        FindFirstCommonNode(listNode1,listNode4);
    }

    public static ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode index1 = pHead1;

        while (true) {
            ListNode index2 = pHead2;
            if (index1 == null) {
                break;
            }
            while (true) {
                if (index2 == null) {
                    break;
                }
                if (index1.val == index2.val) {
                    return index1;
                }
                index2 = index2.next;
            }
            index1 = index1.next;

        }
        return null;
    }
}
